package ai.people.netmon.warning.controller;

import ai.people.core.common.properties.elastic.ElasticIndex;
import ai.people.netmon.framework.domain.warning.request.InfoWarningRequest;
import ai.people.netmon.framework.domain.warning.vo.InstantMessageVO;
import ai.people.netmon.framework.domain.warning.vo.MessageContentVO;
import ai.people.netmon.framework.domain.warning.vo.NewsMessagingVO;
import ai.people.netmon.framework.domain.warning.vo.SocialMessageVO;
import ai.people.netmon.framework.exception.enums.SourceExceptionEnum;
import ai.people.netmon.framework.exception.type.BusinessException;
import ai.people.netmon.framework.model.response.PageResponse;
import ai.people.netmon.framework.model.response.Result;
import ai.people.netmon.framework.utils.ResultUtil;
import ai.people.netmon.warning.service.WarningInfoService;
import ai.people.netmon.warning.service.WarningSpeechTemplateService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 信息舆情接口
 *
 * @author yuanqinglong
 * @date 2022/7/27 18:08
 */
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/info")
public class WarningInfoController {


    private final WarningInfoService warningInfoService;

    private final WarningSpeechTemplateService<InfoWarningRequest, MessageContentVO> warningInfoServiceImpl;


    /**
     * 信息预警列表
     *
     * @param infoWarningRequest 信息警告请求
     * @return {@link Result}<{@link PageResponse}<{@link List}<{@link MessageContentVO}>>>
     */
    @PostMapping("/list")
    public Result<PageResponse<List<MessageContentVO>>> listInfoWarning(@RequestBody InfoWarningRequest infoWarningRequest) {
        PageResponse<MessageContentVO> listPageResponse = warningInfoServiceImpl.listMessages(infoWarningRequest);
        return ResultUtil.success(listPageResponse);
    }


    /**
     * 查看详细信息统一入口
     *
     * @param platform 平台
     * @param id       id
     * @return {@link Result}<{@link Object}>
     */
    @GetMapping("/{platform}/{id}")
    public Result<?> messageDetail(@PathVariable("platform") String platform, @PathVariable("id") String id) {
        String category = ElasticIndex.getPlatformClassification(platform);
        switch (category) {
            case "im":
                // 即时通讯
                return this.instantMessagingDetail(platform, id);
            case "social":
                return this.socialMessagingDetail(platform, id);
            case "web":
                return this.newsMessagingDetail(platform, id);
            default:
                throw new BusinessException(SourceExceptionEnum.INVALID_PLATFORM);
        }
    }


    /**
     * 即时消息详情(提供接口文档-请求访问查看详细信息统一入口)
     *
     * @param platform 平台
     * @param id       id
     * @return {@link Result}<{@link Object}>
     */
    @GetMapping("/instant/{platform}/{id}")
    public Result<InstantMessageVO> instantMessagingDetail(@PathVariable("platform") String platform, @PathVariable("id") String id) {
        return ResultUtil.success(warningInfoService.messageDetail(platform, id, InstantMessageVO.class));
    }


    /**
     * 新闻详细信息(提供接口文档-请求访问查看详细信息统一入口)
     *
     * @param platform 平台
     * @param id       id
     * @return {@link Result}<{@link Object}>
     */
    @GetMapping("/news/{platform}/{id}")
    public Result<NewsMessagingVO> newsMessagingDetail(@PathVariable("platform") String platform, @PathVariable("id") String id) {
        return ResultUtil.success(warningInfoService.messageDetail(platform, id, NewsMessagingVO.class));
    }


    /**
     * 社交平台信息详情(提供接口文档-请求访问查看详细信息统一入口)
     *
     * @param platform 平台
     * @param id       id
     * @return {@link Result}<{@link SocialMessageVO}>
     */
    @GetMapping("/social/{platform}/{id}")
    public Result<SocialMessageVO> socialMessagingDetail(@PathVariable("platform") String platform, @PathVariable("id") String id) {
        return ResultUtil.success(warningInfoService.messageDetail(platform, id, SocialMessageVO.class));
    }


}
